package com.video.ui.video;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.video.R;

import java.util.List;

public class RVAdp extends RecyclerView.Adapter<RVAdp.VH> {
    public static MediaPlayer mp;
    private final List<VideoBean> mList;
    private Context context;
    public RVAdp(Context context, List<VideoBean> mList) {
        this.mList = mList;
        this.context=context;
        mp=new MediaPlayer();
    }
    @NonNull
    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) { return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.video_item, parent, false)); }
    @Override
    public void onBindViewHolder(@NonNull VH viewHolder, int position) {}
    @Override
    public void onViewAttachedToWindow(@NonNull VH vh) {
        super.onViewAttachedToWindow(vh);
        vh.sv.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                try { if (mp.isPlaying()){
                    mp.stop();}
                    mp.reset();
                    mp.setDataSource(mList.get(vh.getAdapterPosition()).path);
                    mp.prepareAsync();
                    mp.setLooping(true);
                } catch (Exception exception){
                    exception.printStackTrace();
                }
                mp.setOnPreparedListener(Mp->{
                    changeVideoSize(context,mp,vh.sv);
                    mp.setDisplay(holder);
                    mp.start();
                    vh.sb.setMax(mp.getDuration());
                });
            }
            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) { }
            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) { }
        });
        vh.sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser){ mp.seekTo(progress); }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }
    @Override
    public int getItemCount(){return mList.size();}
    public void changeVideoSize(Context context,MediaPlayer mp,SurfaceView sv) {
        int videoWidth = mp.getVideoWidth();
        int videoHeight = mp.getVideoHeight();
        float max;
        if (context.getResources().getConfiguration().orientation== ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            max = Math.max((float) videoWidth / (float) sv.getWidth(),(float) videoHeight / (float)  sv.getHeight());
        } else{
            max = Math.max(((float) videoWidth/(float) sv.getHeight()),(float) videoHeight/(float) sv.getWidth());
        }
        videoWidth = (int) Math.ceil((float) videoWidth / max);
        videoHeight = (int) Math.ceil((float) videoHeight / max);
        sv.setLayoutParams(new LinearLayout.LayoutParams(videoWidth, videoHeight));
    }
    static class VH extends RecyclerView.ViewHolder {
        SurfaceView sv;
        SeekBar sb;
        public VH(View itemView) {
            super(itemView);
            sv=itemView.findViewById(R.id.sv);
            sb=itemView.findViewById(R.id.sb);
        }
    }
}